package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.ResponsedDevice;
import com.alan.libapi.model.business.UserBoundDevice;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVCNTBindLink;

/**
 * @className: CNTBindLinkPS
 * @describtion: CNTBindLinkPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 19:10
 */
public class CNTBindLinkPS {

    private IVCNTBindLink ivcntBindLink;

    public CNTBindLinkPS(IVCNTBindLink ivcntBindLink) {
        this.ivcntBindLink = ivcntBindLink;
    }

    /**
     * 绑定设备
     */
    public void bindDevice() {
        final String deviceInfo = ivcntBindLink.getDeviceInfo();
        if(deviceInfo == null || TextUtils.isEmpty(deviceInfo)){
            ivcntBindLink.toastMsg("设备不能为空！");
            return;
        }
        ivcntBindLink.showProgress();
        GlobalField.businessHttp.postBindDevice(GlobalField.userSession, deviceInfo, new CallbackListener<ResponsedDevice>() {
            @Override
            public void onSuccess(ResponsedDevice result) {
                UserBoundDevice userBoundDevice = new UserBoundDevice();
                userBoundDevice.setDeviceId(result.getDeviceId());
                userBoundDevice.setDeviceMac(deviceInfo);
                GlobalField.userBoundDevice = userBoundDevice;
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivcntBindLink.hideProgress();
                        ivcntBindLink.bindDeviceSuccess();
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivcntBindLink.hideProgress();
                        ivcntBindLink.toastMsg(msg);
                    }
                });
            }
        });
    }
}
